{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e26bf59c",
   "metadata": {},
   "source": [
    "# week14内容\n",
    "> 1. 函数的定义\n",
    "> 2. 如何使用函数\n",
    "> 3. 函数的参数\n",
    "> 4. 函数的返回值\n",
    "> 5. 传递任意数量的实参\n",
    "> 6. 函数的模块"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c774d3d8",
   "metadata": {},
   "source": [
    "# 1.函数的定义\n",
    "```\n",
    "def:定义函数的关键字\n",
    "fib：函数名\n",
    "(n)：定义函数的参数\n",
    "\"\"\"\"\"\"：文档描述\n",
    "代码块：\n",
    "     a, b = 0, 1\n",
    "     while a < n:\n",
    "        print(a, end=' ')\n",
    "         a, b = b, a+b\n",
    "     print()\n",
    "```\n",
    "* 定义函数：\n",
    "> 1. 写def：def定义函数的第一步\n",
    "> 2. 写定义函数的名称：def function_name\n",
    ">> * function_name 遵循变量名称定义的规则\n",
    "> 3. 函数名称后加上\"():\" : def function_name(): ===> f(x) = 2x\n",
    "* 书写函数体：\n",
    "> 1. 函数的注释\n",
    "> 2. 写函数的具体操作（函数体）\n",
    "> 3. 注意：函数也是需要有tab键的缩进\n",
    "* 使用函数(函数的调用)\n",
    "> 1. function_ name()\n",
    "* 函数存在的意义"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "f0481492",
   "metadata": {},
   "outputs": [],
   "source": [
    "def fib(n):\n",
    "    a,b = 0,1\n",
    "    while a < n:\n",
    "        print(a, end='')\n",
    "        a, b= b, a+b\n",
    "    print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "f70625e3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0112358\n"
     ]
    }
   ],
   "source": [
    "fib(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "cc6fa1ac",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实现hello world的函数\n",
    "def hello():\n",
    "    \"\"\"打印hello world\"\"\"\n",
    "    print(\"hello world\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "fe7e65d9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world\n"
     ]
    }
   ],
   "source": [
    "hello()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dba47961",
   "metadata": {},
   "source": [
    "# 2.使用函数（课本实践149页）\n",
    "* 函数存在的意义\n",
    "> 1. 可以通过调用函数的方式实现某个功能，使用人不需要考虑函数代码内容\n",
    "> 2. 节省时间，提升应用的效率\n",
    "> 3. 函数一旦定义，就可按照一定的规则重复使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "cb5a351c",
   "metadata": {},
   "outputs": [],
   "source": [
    "def search4vowels(word):\n",
    "    \"\"\"打印文本中出现的元音字母\"\"\"\n",
    "    vowels = set ('aeiou')\n",
    "    found = vowels.intersection(set(word))\n",
    "    print(found)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "fe663d44",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'e', 'i', 'o'}\n"
     ]
    }
   ],
   "source": [
    "search4vowels(\"eqfhweqioff\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "68d2cdb0",
   "metadata": {},
   "source": [
    "* 与其他方法进行对比，以下的方法使用起来会感觉重复，主要原因是没有对重复功能的代码块有特殊的定义（如：名称，注释等）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e92fb21a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:gdqigdwqugtd\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'i', 'u'}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels = set('aeiou')\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = vowels.intersection(set(word))\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b9b7de71",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:iuoahihwihfwehewh\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'a', 'e', 'i', 'o', 'u'}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels = set('aeiou')\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = vowels.intersection(set(word))\n",
    "found"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "60396c2d",
   "metadata": {},
   "source": [
    "# 3.函数的参数 \n",
    "* 注意：函数的参数可以没有，如果有参数，则如下（分为形式参数和实际参数）\n",
    "> 1. 形式参数(形参)\n",
    ">> 1. 在定义过程中，函数的参数为形式参数\n",
    ">> 2. 形式参数是虚拟的，不占用空间的\n",
    "> 2. 实际参数(实参)\n",
    ">> 1. 调用函数过程中，函数的参数为实际参数\n",
    ">> 2. 调用函数时，形式参数被分配空间，成为实际参数并应用起来"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "50598586",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world\n"
     ]
    }
   ],
   "source": [
    "# 函数是可以没有参数的\n",
    "hello()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "2615fbcd",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义函数\n",
    "def greet_user(username):    \n",
    "    \"\"\"显示简单的问候语，输入你的名字\"\"\"    \n",
    "    print(\"hello,\"+ username.title()+\"!\") "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "24f47b73",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello,Jiezhi!\n"
     ]
    }
   ],
   "source": [
    "greet_user(\"jiezhi\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "55614258",
   "metadata": {},
   "source": [
    "* 当参数有很多个时：\n",
    "> 1. 位置参数\n",
    ">> 1. 形式参数有多个值，如果实际参数直接赋值，要按照形式参数的先后顺序，这种方式称：位置参数赋值\n",
    ">> 2. 在调用函数过程中需要考虑因素如下：\n",
    ">>> 1. 考虑位置\n",
    ">>> 2. 考虑个数\n",
    "> 2. 关键字因素\n",
    ">> 1. 在调用函数时使用，我们直接在实参中 用赋值的方式去定义形参\\\n",
    ">> 2. 不需要考虑顺序，因为赋值已经确定了形式参数的值\n",
    "> 3. 默认参数 \n",
    ">> 1. 默认参数写在 形式参数中（预计有大量的调用会使用到的值，可以预先设置）\n",
    ">> 2. 默认参数存在的话，实际调用过程中可以省略赋值的过程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "f8ca0359",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 两个参数\n",
    "def describe_pet(animal_type,pet_name):\n",
    "    \"\"\"显示宠物信息\"\"\"\n",
    "    print(\"\\nI have a \" + animal_type + \".\")\n",
    "    print(\"My \" + animal_type + \"'s name is \"+pet_name.title() + \".\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "b045a8fa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have a rabbit.\n",
      "My rabbit's name is 兔兔.\n"
     ]
    }
   ],
   "source": [
    "describe_pet(\"rabbit\",\"兔兔\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "142e0998",
   "metadata": {},
   "outputs": [],
   "source": [
    "describe_pet(\"cat\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "bb176bd3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have a rabbit.\n",
      "My rabbit's name is 兔兔.\n"
     ]
    }
   ],
   "source": [
    "# 关键字参数\n",
    "describe_pet(animal_type=\"rabbit\",pet_name=\"兔兔\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "1b3df93a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have a rabbit.\n",
      "My rabbit's name is 兔兔.\n"
     ]
    }
   ],
   "source": [
    "describe_pet(pet_name=\"兔兔\",animal_type=\"rabbit\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "17a7dc02",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 默认参数(写在形式参数中的)\n",
    "def describe_pet(pet_name,animal_type=\"rabbit\"):\n",
    "    \"\"\"显示宠物信息，animal_type 的默认值 'rabbit'\"\"\"\n",
    "    print(\"\\nI have a \" + animal_type + \".\")\n",
    "    print(\"My \" + animal_type + \"'s name is \"+pet_name.title() + \".\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "eecebb1d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have a rabbit.\n",
      "My rabbit's name is 兔兔.\n"
     ]
    }
   ],
   "source": [
    "describe_pet(\"兔兔\",\"rabbit\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "130c3b95",
   "metadata": {},
   "source": [
    "* 实操和注意（参数部分）positional argument follows keyword argument\n",
    "> 1. 如果想关键字参数和位置参数共同使用\n",
    ">> 1. positional argument follows keyword argument(位置参数需要放在关键字参数的前面)\n",
    ">> 2. 关键字参数放在末尾（均是关键字，不考虑顺序）\n",
    "> 2. 默认参数\n",
    ">> 1. non-default argument follows default argument(非默认参数需要放在默认参数的前面)\n",
    ">> 2. 默认参数放在末尾（均是默认参数，不考虑顺序）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "1afa619f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "def func_test(x,y):\n",
    "    print(x)\n",
    "    print(y)\n",
    "    \n",
    "func_test(2,2)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "828ce04a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "5\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "def func_test(x,y,z):\n",
    "    print(x)\n",
    "    print(y)\n",
    "    print(z)\n",
    "    \n",
    "func_test(2,5,z=3)    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "2ed10b85",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "3\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "def func_test(x,z,y=4,):\n",
    "    print(x)\n",
    "    print(y)\n",
    "    print(z)\n",
    "    \n",
    "func_test(2,1,3) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "75b1badc",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
